import { observable, action } from 'mobx';
import API from '../../apis/widget/todo';

import { Bubble, Tools} from 'weaver-mobile';
import { Form, SelectPage } from 'weaver-mobile-page';
const getLabel = Tools.getLabel;

export default class TodoStore {
  path;
  @observable params;
  @observable form;
  @observable selectPage;
  @observable condition = [];
  @observable selectArr = [];

  @action
  setState = (params = {}) => {
    Object.keys(params).forEach(key => {
      this[key] = params[key];
    });
    return this;
  }

  @action doInit = (path, params = {}) => {
    this.path = path;
    this.params = params;
    this.form = new Form.Store({ route: path });
    this.selectPage = new SelectPage.Store({ route: path })
    this.getCondition(path, params);
  }

  @action
  getCondition = (path, params) => {
    API.getCondition().then(res => {
      this.condition = res.conditioninfo;
      Promise.all([
        this.form.initForm(res.conditioninfo, path),
      ]).then(() => {
        let { key, formParams } = params;
        let datas = {};
        this.selectArr = this.form.formParams.waitdealway !== '' ? this.form.formParams.waitdealway.split(',') : ['5'];
        if (formParams) {
          Object.keys(formParams).map(key => {
            return datas = { ...datas, [key]: { value: formParams[key] } };
          });
          if (formParams.waitdealway) {
            this.selectArr = formParams.waitdealway.split(',');
          } else {
            this.selectArr = ['5']
            datas = {...datas, waitdealway: { value: "5" }}
          }
        }
        if (key && key === 'addRemind') {
          datas = { ...datas, op_hasRemind : { value: 1 }, waitdealway: { value: "5" } };
        }
        if (key && key === 'addNote') {
          datas = { ...datas, op_hasNote : { value: 1 } };
        }
        Object.keys(datas).length > 0 && this.form.updateFields({ ...datas });
      })
    })
  }

  @action
  addOrUpdateWaitDeal = (id, callback) => {
    const params = {
      ...this.form.formParams,
      mailIds: id,
    }
    API.addOrUpdateWaitDeal(params).then(res => {
      if (res.status === '1') {
        if (callback) callback();
        Bubble.success(getLabel(83551, '保存成功!'));
      } else {
        Bubble.error(getLabel(84544, '保存失败!'));
      }
    })
  }
}